package com.example.administrator.boltshow;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import adapter.LoginTabPagerAdapter;
import fragment.LoginFragment;
import fragment.RegisterFragment;

public class LoginActivity extends AppCompatActivity {

    private ActionBar mActionBar;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<String> titles;
    private List<Fragment> fragments;
    private LoginTabPagerAdapter mAdapter;
    private ImageView mImageViewClose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        mImageViewClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setData() {
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        Intent intent = getIntent();
        int flag = intent.getIntExtra("flag", 1);
        if(flag==2) {
            mViewPager.setCurrentItem(1);
        }
        mTabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
    }

    private void initData() {


        titles = new ArrayList<>();
        fragments = new ArrayList<>();
        titles.add("登录");
        titles.add("注册");
        for (int i = 0; i < titles.size(); i++) {
            if(i==0) {
                fragments.add(new LoginFragment());
            }else if(i==1) {
                fragments.add(new RegisterFragment());
            }
        }
        mAdapter = new LoginTabPagerAdapter(getSupportFragmentManager(),fragments,titles);

    }

    private void initView() {
        mTabLayout = (TabLayout) findViewById(R.id.tl_login_title);
        mViewPager = (ViewPager) findViewById(R.id.vp_login_content);
        mActionBar = getSupportActionBar();
        mActionBar.hide();
        mImageViewClose = (ImageView) findViewById(R.id.iv_login_close);
    }
}
